<template>
  <div>
    <el-card>
      商品搜索:
      <el-input
        placeholder="请输入"
        style="width: 200px; margin-right: 20px"
        v-model="params.skuName"
      ></el-input>
      <el-button type="primary" @click="searchFn">查询</el-button>
    </el-card>
    <el-card>
      <el-button style="background-color: orange; color: white" @click="addFn"
        >新建</el-button
      >
      <el-table :data="tableData">
        <el-table-column prop="indexId" label="序号"></el-table-column>
        <el-table-column prop="skuName" label="商品名称"></el-table-column>
        <el-table-column label="商品图片">
          <template #default="scope">
            <img
              :src="scope.row.skuImage"
              alt="图片未显示"
              style="width: 40px; height: 40px"
            />
          </template>
        </el-table-column>
        <el-table-column prop="brandName" label="品牌"></el-table-column>
        <el-table-column prop="unit" label="规格"></el-table-column>
        <el-table-column prop="price" label="商品价格"></el-table-column>
        <el-table-column
          prop="skuClass.className"
          label="商品类型"
        ></el-table-column>
        <el-table-column prop="createTime" label="创建日期"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="text" @click="edit(scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        @current-change="changeFn"
      >
      </el-pagination>
    </el-card>
    <!-- 添加/删除对话框 -->
    <el-dialog :before-close="handleClose" :title="title" :visible.sync="dialogVisible" width="50%">
      <el-form ref="formRef">
        <el-form-item>
          商品名称:
          <el-input
            type="text"
            placeholder="请输入内容"
            v-model="list.skuName"
            maxlength="15"
            minlength="4"
            show-word-limit
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          品牌:<el-input
            type="text"
            placeholder="请输入内容"
            v-model="list.brandName"
            maxlength="10"
            minlength="4"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item>
          商品价格(元):
          <el-input
            type="text"
            placeholder="请输入内容"
            v-model="list.price"
            maxlength="10"
            minlength="4"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item>
          商品类型:<el-select
            v-model="list.classId"
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="item in options"
              :key="item.classId"
              :label="item.className"
              :value="item.classId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          规格:<el-input
            type="text"
            placeholder="请输入内容"
            v-model="list.unit"
            maxlength="10"
            minlength="5"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item>
          商品图片:<el-upload
            class="avatar-uploader"
            action="/likede/api/vm-service/sku/fileUpload"
            :show-file-list="true"
            name='fileName'
            :on-success='fileSucessFn'
          >
            <img v-if="list.skuImage" :src="list.skuImage" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <!-- <span slot="footer" class="dialog-footer"> -->
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmFn">确 定</el-button>
          <!-- </span> -->
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  getSpglSearch,
  getSpglEdit,
  getSplxList,
  getSpglAdd
} from '@/api/spgl'
export default {
  data () {
    return {
      dialogVisible: false,
      tableData: [],
      list: {
        pageIndex: 1,
        pageSize: 10,
        skuName: '',
        classId: ''
      },
      options: [],
      title: '修改商品',
      params: {
        pageIndex: 1,
        pageSize: 10,
        skuName: '',
        classId: ''
      },
      total: 1
    }
  },
  methods: {
    handleClose (done) {
      this.list = {}
      done()
    },
    fileSucessFn (response, file, fileList) {
      this.list.skuImage = response
    },
    async getSplxList () {
      const res = await getSplxList(this.params)
      console.log(res.data.currentPageRecords)
      this.options = res.data.currentPageRecords
    },
    async getSpglSearch () {
      const res = await getSpglSearch(this.params)
      // console.log(res.data)
      this.tableData = res.data.currentPageRecords.map((item, index) => ({
        ...item,
        indexId: index + 1
      }))
      this.total = res.data.totalCount - 0
    },
    searchFn () {
      this.getSpglSearch()
    },
    edit (row) {
      console.log(row)
      this.title = '修改商品'
      this.dialogVisible = true
      this.list = { ...row }
      // console.log(this.list)
    },
    async confirmFn () {
      // console.log(this.list.className);
      if (this.title === '修改商品') {
        const res = await getSpglEdit(
          {
            skuName: this.list.skuName,
            skuImage: this.list.skuImage,
            price: this.list.price,
            classId: this.list.classId,
            unit: this.list.unit,
            brandName: this.list.brandName,
            skuId: this.list.skuId
          },

          this.list.classId
        )
        // console.log(this), console.log(res)
        // console.log(this.$refs.formRef)
        this.$refs.formRef.resetFields()
        this.list = {}
      } else {
        const res = await getSpglAdd({
          skuName: this.list.skuName,
          skuImage: this.list.skuImage,
          price: this.list.price,
          classId: this.list.classId,
          unit: this.list.unit,
          brandName: this.list.brandName
        })
        console.log(res)
        this.list = {}
      }
      // console.log(res);
      this.getSpglSearch()
      this.dialogVisible = false
    },
    addFn () {
      this.title = '添加商品'
      this.dialogVisible = true
    },
    changeFn (current) {
      this.params.pageIndex = current
      this.getSpglSearch()
    }
  },
  created () {
    this.getSpglSearch()
    this.getSplxList()
  }
}
</script>

<style>
.el-card {
  margin-bottom: 20px;
}
.el-input {
  width: 300px;
}
</style>
